package com.itheima.service;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.SetmealMapper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Setmeal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    SetmealMapper setmealMapper;

    @Override
    public void add(Setmeal setmeal) {
        // 保存套餐基本信息并返回主键
        setmealMapper.add(setmeal);
        // 批量建立关系
        Integer setmealId = setmeal.getId();
        List<Integer> checkgroupIds = setmeal.getCheckgroupIds();
        //insert into 表名（字段1 字段2） values(1,2) ,(1,3),(1,4)

        if(CollectionUtil.isNotEmpty(checkgroupIds)){
            setmealMapper.setSetmealAndCheckGroupBatch(setmealId,checkgroupIds);
        }

    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Page page = PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());

        setmealMapper.findPage(queryPageBean.getQueryString());

        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public List<Setmeal> getSetmeal() {
        return setmealMapper.getSetmeal();
    }

    @Override
    public Setmeal findById(Integer id) {
        //根据套餐id查询套餐详情
       Setmeal setmeal =  setmealMapper.findById(id);
       if(null != setmeal){
           //根据套餐id查询套餐下面所有检查组
            List<CheckGroup> checkGroups = setmealMapper.findCheckGroupBySetmealId(id);
            if(CollectionUtil.isNotEmpty(checkGroups)){
                for (CheckGroup checkGroup : checkGroups) {
                    //循环检查组查询下面的检查项
                  List<CheckItem> checkItems= setmealMapper.findCheckItemByCheckGroupId(checkGroup.getId());
                  checkGroup.setCheckItems(checkItems);
                }
            }
           setmeal.setCheckGroups(checkGroups);
       }
       return setmeal;
    }

    @Override
    public Setmeal findByIdBatch(Integer id) {
        //根据套餐id查询套餐详情
        Setmeal setmeal =  setmealMapper.findById(id);
        if(null != setmeal){
            //根据套餐id查询套餐下面所有检查组
            List<CheckGroup> checkGroups = setmealMapper.findCheckGroupBySetmealId(id);
            if(CollectionUtil.isNotEmpty(checkGroups)){
                List<Integer> ids = new ArrayList<>();
                for (CheckGroup checkGroup : checkGroups) {
                    ids.add(checkGroup.getId());
                }
                List<CheckItem> checkItems= setmealMapper.findCheckItemByCheckGroupIdBatch(ids);
                //手动分组
                Map<Integer,List<CheckItem>> map = new HashMap<>();


//                for (CheckGroup checkGroup : checkGroups) { //7
//                    Integer checkGroupId = checkGroup.getId();
//                    List<CheckItem> items = new ArrayList<>();
//                    for (CheckItem checkItem : checkItems) {//54
//                        if(checkGroupId.equals(checkItem.getCheckGroupId())){
//                            items.add(checkItem);
//                        }
//                    }
//                    map.put(checkGroupId,items);
//                }

//                for (CheckItem checkItem : checkItems) {
//                    Integer checkGroupId = checkItem.getCheckGroupId();
//                    List<CheckItem> items = map.get(checkGroupId);
//                    if(null == items){
//                        items = new ArrayList<>();
//                        map.put(checkGroupId,items);
//                    }
//
//                    items.add(checkItem);
//                }

                //https://segmentfault.com/a/1190000016586700
                map = checkItems.stream().collect(Collectors.groupingBy(CheckItem::getCheckGroupId));


                for (CheckGroup checkGroup : checkGroups) {
                    checkGroup.setCheckItems(map.get(checkGroup.getId()));
                }

            }
            setmeal.setCheckGroups(checkGroups);
        }
        return setmeal;
    }

    @Override
    public Setmeal findDetailById(Integer id) {
        return setmealMapper.findById(id);
    }
}
